import pygame

from statics.color import Color
from statics.font import Font
from statics.game_variable import GameVariable
from statics.text import Text
from utils.game_utils import is_passed, died_judge


# surface_18 = pygame.image.load("D:/Coser.jpg")
# surface_18.set_alpha(120)


def home_page_render(surface):
    """
    主页渲染
    :param surface: 表面
    :return:
    """
    surface.fill(Color.black)
    # surface.blit(surface_18, (0, 0))
    surface.blit(Text.text_title, (320, 150))
    if GameVariable.home_menu_num == 4:
        surface.blit(Text.text_one_player, (365, 310))
        surface.blit(Text.text_two_players, (365, 360))
        surface.blit(Text.text_help, (365, 410))
        surface.blit(Text.text_exit, (365, 460))
    if GameVariable.home_menu_num == 5:
        surface.blit(Text.text_load_data, (365, 310))
        surface.blit(Text.text_one_player, (365, 360))
        surface.blit(Text.text_two_players, (365, 410))
        surface.blit(Text.text_help, (365, 460))
        surface.blit(Text.text_exit, (365, 510))

    for i in range(5):
        if GameVariable.home_cursor_line == i + 1:
            surface.blit(Text.text_left, (340, 318 + i * 50))
            surface.blit(Text.text_right, (450, 318 + i * 50))


def pause_page_render(screen, surface):
    """
    向屏幕上渲染暂停页面
    :param screen: 屏幕对象
    :param surface: 装载页面元素的表面
    :return:
    """
    surface.fill((59, 85, 86))
    surface.blit(Text.text_pause, (295, 150))
    surface.blit(Text.text_continue, (370, 400))
    surface.blit(Text.text_save, (370, 450))
    surface.blit(Text.text_index, (370, 500))
    for i in range(3):
        if GameVariable.pause_cursor_line == i + 1:
            surface.blit(Text.text_left, (345, 408 + i * 50))
            surface.blit(Text.text_right, (455, 408 + i * 50))
    screen.blit(surface, (0, 0))


def success_page_render(surface):
    """
    向屏幕上渲染暂停页面
    :param surface: 装载页面元素的表面
    :return:
    """
    surface.fill((0, 0, 0))
    surface.blit(Text.text_success, (295, 150))
    surface.blit(Text.text_next, (360, 400))
    surface.blit(Text.text_save, (370, 450))
    surface.blit(Text.text_index, (370, 500))
    if GameVariable.success_cursor_line == 1:
        surface.blit(Text.text_left, (335, 408))
        surface.blit(Text.text_right, (460, 408))
    if GameVariable.success_cursor_line == 2:
        surface.blit(Text.text_left, (345, 458))
        surface.blit(Text.text_right, (455, 458))
    if GameVariable.success_cursor_line == 3:
        surface.blit(Text.text_left, (345, 508))
        surface.blit(Text.text_right, (455, 508))


def game_page_render(surface):
    """
    游戏界面渲染，以及游戏逻辑处理
    :param surface: 表面
    :return:
    """
    surface.fill((0, 0, 0))

    level_text = Font.common_text.render(str(GameVariable.current_level), True, Color.light_blue)
    player1_score_text = Font.common_text.render(str(GameVariable.player1_score), True, Color.light_green)
    player2_score_text = Font.common_text.render(str(GameVariable.player2_score), True, Color.light_green)

    # draw text
    if GameVariable.player_num == 1:
        # draw level text
        surface.blit(Text.text_level, (160, 40))
        surface.blit(level_text, (230, 40))

        # draw score text
        surface.blit(Text.text_player1_score, (530, 40))
        surface.blit(player1_score_text, (650, 40))
    if GameVariable.player_num == 2:
        # draw level text
        surface.blit(Text.text_level, (326, 40))
        surface.blit(level_text, (450, 40))

        # draw score text
        surface.blit(Text.text_player1_score, (50, 40))
        surface.blit(player1_score_text, (180, 40))
        surface.blit(Text.text_player2_score, (583, 40))
        surface.blit(player2_score_text, (700, 40))

    # draw players
    for player in GameVariable.players:
        if not player.is_destroyed:
            player.update()

            # draw body
            for coord in player.passed_coordinates:
                pygame.draw.circle(surface, player.bodies[0].color, coord, player.bodies[0].radius)

            # draw head
            pygame.draw.circle(surface, player.header.color, player.header.rect.center, player.header.radius)

    # draw beans
    for bean in GameVariable.beans:
        pygame.draw.circle(surface, bean.color, bean.rect.center, bean.radius)

        # players collide with beans
        for idx, player in enumerate(GameVariable.players):
            if player.header.rect.colliderect(bean.rect):
                player.eat()
                if idx == 0:
                    GameVariable.player1_score += 1
                if idx == 1:
                    GameVariable.player2_score += 1

                if is_passed():
                    GameVariable.current_page = GameVariable.success_page
                bean.destroy()
                bean.spawn(GameVariable.players, GameVariable.walls)

    # draw walls
    for wall in GameVariable.walls:
        pygame.draw.rect(surface, wall.color, wall)

    died_judge(GameVariable.players, GameVariable.walls)


def readme_page_render(surface):
    surface.fill(Color.black)
    for idx, text in enumerate(Text.text_readme_details):
        surface.blit(text, (50, 70 + 40 * idx))
    surface.blit(Text.text_any_key_continue, (630, 550))


def died_page_render(screen, surface):
    surface.fill(Color.failure)
    surface.blit(Text.text_failure, (295, 150))
    surface.blit(Text.text_continue, (370, 400))
    surface.blit(Text.text_index, (370, 450))
    for i in range(2):
        if GameVariable.died_cursor_line == i + 1:
            surface.blit(Text.text_left, (345, 408 + i * 50))
            surface.blit(Text.text_right, (455, 408 + i * 50))
    screen.blit(surface, (0, 0))